<!DOCTYPE HTML>
<html lang="en" >
    <!-- Start book 美多商城 -->
    <head>
        <!-- head:start -->
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>第二步:获取openID | 美多商城</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.css">
        
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-search/search.css">
        
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="../../C02-Users/QQLogin/Step3.html" />
    
    
    <link rel="prev" href="../../C02-Users/QQLogin/Step1.html" />
    

        <!-- head:end -->
    </head>
    <body>
        <!-- body:start -->
        
    <div class="book"
        data-level="2.7.2"
        data-chapter-title="第二步:获取openID"
        data-filepath="C02-Users/QQLogin/Step2.md"
        data-basepath="../.."
        data-revision="Sat Sep 01 2018 17:18:16 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        美多商城
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="C01-Prepare/index.html">
            
                
                    <a href="../../C01-Prepare/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        项目准备
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="C01-Prepare/BusinessModel.html">
            
                
                    <a href="../../C01-Prepare/BusinessModel.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                        商业模式
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="C01-Prepare/DevelopmentProcess.html">
            
                
                    <a href="../../C01-Prepare/DevelopmentProcess.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.</b>
                        
                        开发流程
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="C01-Prepare/ProductAnalysis.html">
            
                
                    <a href="../../C01-Prepare/ProductAnalysis.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.3.</b>
                        
                        需求分析
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="C01-Prepare/ProjectStructure.html">
            
                
                    <a href="../../C01-Prepare/ProjectStructure.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.4.</b>
                        
                        项目架构
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="C01-Prepare/StartProject.html">
            
                
                    <a href="../../C01-Prepare/StartProject.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.5.</b>
                        
                        创建工程
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="C01-Prepare/Config.html">
            
                
                    <a href="../../C01-Prepare/Config.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.6.</b>
                        
                        配置
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2" data-path="C02-Users/index.html">
            
                
                    <a href="../../C02-Users/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        用户部分
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="C02-Users/UserModel.html">
            
                
                    <a href="../../C02-Users/UserModel.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        用户模型类
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="C02-Users/Domain.html">
            
                
                    <a href="../../C02-Users/Domain.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        设置域名
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="C02-Users/CORS.html">
            
                
                    <a href="../../C02-Users/CORS.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        跨域CORS
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="C02-Users/RegisterAnalysis.html">
            
                
                    <a href="../../C02-Users/RegisterAnalysis.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                        注册
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.4.1" data-path="C02-Users/SMSCode.html">
            
                
                    <a href="../../C02-Users/SMSCode.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.1.</b>
                        
                        短信验证码
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.4.2" data-path="C02-Users/CelerySendSMSCode.html">
            
                
                    <a href="../../C02-Users/CelerySendSMSCode.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.2.</b>
                        
                        使用Celery发送短信
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.4.3" data-path="C02-Users/AccountExistingCheck.html">
            
                
                    <a href="../../C02-Users/AccountExistingCheck.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.3.</b>
                        
                        判断帐号是否存在
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.4.4" data-path="C02-Users/Register.html">
            
                
                    <a href="../../C02-Users/Register.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.4.</b>
                        
                        注册
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="C02-Users/JWT.html">
            
                
                    <a href="../../C02-Users/JWT.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.5.</b>
                        
                        JWT
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.5.1" data-path="C02-Users/DRF-JWT.html">
            
                
                    <a href="../../C02-Users/DRF-JWT.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.5.1.</b>
                        
                        drf JWT
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.6" data-path="C02-Users/Login.html">
            
                
                    <a href="../../C02-Users/Login.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.6.</b>
                        
                        登录
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.7" data-path="C02-Users/QQLogin/index.html">
            
                
                    <a href="../../C02-Users/QQLogin/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.7.</b>
                        
                        QQ登录
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.7.1" data-path="C02-Users/QQLogin/Step1.html">
            
                
                    <a href="../../C02-Users/QQLogin/Step1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.7.1.</b>
                        
                        第一步:获取url
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="2.7.2" data-path="C02-Users/QQLogin/Step2.html">
            
                
                    <a href="../../C02-Users/QQLogin/Step2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.7.2.</b>
                        
                        第二步:获取openID
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.7.3" data-path="C02-Users/QQLogin/Step3.html">
            
                
                    <a href="../../C02-Users/QQLogin/Step3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.7.3.</b>
                        
                        第三步:绑定账户
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.7.4" data-path="C02-Users/QQLogin/Step4.html">
            
                
                    <a href="../../C02-Users/QQLogin/Step4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.7.4.</b>
                        
                        itsdangerous
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.8" data-path="C02-Users/UserInfo.html">
            
                
                    <a href="../../C02-Users/UserInfo.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.8.</b>
                        
                        用户中心个人信息
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.9" data-path="C02-Users/Email/index.html">
            
                
                    <a href="../../C02-Users/Email/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.9.</b>
                        
                        邮件与验证
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.9.1" data-path="C02-Users/Email/DjangoEmail.html">
            
                
                    <a href="../../C02-Users/Email/DjangoEmail.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.9.1.</b>
                        
                        Django发送邮件的方法
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.9.2" data-path="C02-Users/Email/SendEmail.html">
            
                
                    <a href="../../C02-Users/Email/SendEmail.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.9.2.</b>
                        
                        保存邮箱并发送验证邮件
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.9.3" data-path="C02-Users/Email/VerifyEmail.html">
            
                
                    <a href="../../C02-Users/Email/VerifyEmail.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.9.3.</b>
                        
                        验证邮箱链接
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.10" data-path="C02-Users/Address/index.html">
            
                
                    <a href="../../C02-Users/Address/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.10.</b>
                        
                        收货地址
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.10.1" data-path="C02-Users/Address/AreaSelect.html">
            
                
                    <a href="../../C02-Users/Address/AreaSelect.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.10.1.</b>
                        
                        省市区地址查询
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.10.2" data-path="C02-Users/Address/Cache.html">
            
                
                    <a href="../../C02-Users/Address/Cache.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.10.2.</b>
                        
                        使用缓存
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.10.3" data-path="C02-Users/Address/UserAddressManagement.html">
            
                
                    <a href="../../C02-Users/Address/UserAddressManagement.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.10.3.</b>
                        
                        用户地址管理
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.10.4" data-path="C02-Users/Address/UserAddressCode.html">
            
                
                    <a href="../../C02-Users/Address/UserAddressCode.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.10.4.</b>
                        
                        用户地址代码
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="C03-Goods/index.html">
            
                
                    <a href="../../C03-Goods/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        商品部分
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="C03-Goods/DatabaseDesign.html">
            
                
                    <a href="../../C03-Goods/DatabaseDesign.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        数据表设计
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1.1" data-path="C03-Goods/DatabaseGoods.html">
            
                
                    <a href="../../C03-Goods/DatabaseGoods.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.1.</b>
                        
                        商品模型类
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.1.2" data-path="C03-Goods/DatabaseContents.html">
            
                
                    <a href="../../C03-Goods/DatabaseContents.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.2.</b>
                        
                        广告模型类
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.1.3" data-path="C03-Goods/DatabaseMigrate.html">
            
                
                    <a href="../../C03-Goods/DatabaseMigrate.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.3.</b>
                        
                        迁移
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="C03-Goods/FastDFS.html">
            
                
                    <a href="../../C03-Goods/FastDFS.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        FastDFS分布式文件系统
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="C03-Goods/Docker/index.html">
            
                
                    <a href="../../C03-Goods/Docker/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                        Docker使用
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.3.1" data-path="C03-Goods/Docker/Introduce.html">
            
                
                    <a href="../../C03-Goods/Docker/Introduce.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.1.</b>
                        
                        Docker简介
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3.2" data-path="C03-Goods/Docker/InstallAndOperations.html">
            
                
                    <a href="../../C03-Goods/Docker/InstallAndOperations.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.2.</b>
                        
                        安装与操作
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="C03-Goods/FastDFSDocker.html">
            
                
                    <a href="../../C03-Goods/FastDFSDocker.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.4.</b>
                        
                        使用Docker安装FastDFS
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.5" data-path="C03-Goods/FastDFSClientAndDjangoFileSystem.html">
            
                
                    <a href="../../C03-Goods/FastDFSClientAndDjangoFileSystem.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.5.</b>
                        
                        FastDFS客户端与自定义文件存储系统
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.6" data-path="C03-Goods/CKEditor.html">
            
                
                    <a href="../../C03-Goods/CKEditor.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.6.</b>
                        
                        CKEditor富文本编辑器
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.7" data-path="C03-Goods/ImportGoodsData.html">
            
                
                    <a href="../../C03-Goods/ImportGoodsData.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.7.</b>
                        
                        添加测试数据
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.8" data-path="C03-Goods/FreezeHTML.html">
            
                
                    <a href="../../C03-Goods/FreezeHTML.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.8.</b>
                        
                        页面静态化
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.9" data-path="C03-Goods/DjangoCrontab.html">
            
                
                    <a href="../../C03-Goods/DjangoCrontab.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.9.</b>
                        
                        定时任务
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.10" data-path="C03-Goods/CategoryQuery.html">
            
                
                    <a href="../../C03-Goods/CategoryQuery.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.10.</b>
                        
                        查询分类的封装
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.11" data-path="C03-Goods/GoodsList.html">
            
                
                    <a href="../../C03-Goods/GoodsList.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.11.</b>
                        
                        商品列表页
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.12" data-path="C03-Goods/GoodsSearch.html">
            
                
                    <a href="../../C03-Goods/GoodsSearch.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.12.</b>
                        
                        商品搜索
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.13" data-path="C03-Goods/GoodsDetail.html">
            
                
                    <a href="../../C03-Goods/GoodsDetail.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.13.</b>
                        
                        商品详情页面
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.14" data-path="C03-Goods/UserBrowseHistory.html">
            
                
                    <a href="../../C03-Goods/UserBrowseHistory.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.14.</b>
                        
                        用户浏览历史记录
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4" data-path="C04-Cart/index.html">
            
                
                    <a href="../../C04-Cart/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        购物车部分
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1" data-path="C04-Cart/DataStructureDesign.html">
            
                
                    <a href="../../C04-Cart/DataStructureDesign.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.</b>
                        
                        购物车数据存储设计
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="C04-Cart/Add.html">
            
                
                    <a href="../../C04-Cart/Add.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.2.</b>
                        
                        购物车增加
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="C04-Cart/Query.html">
            
                
                    <a href="../../C04-Cart/Query.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.3.</b>
                        
                        购物车查询
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="C04-Cart/Modify.html">
            
                
                    <a href="../../C04-Cart/Modify.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.</b>
                        
                        购物车修改
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.5" data-path="C04-Cart/Delete.html">
            
                
                    <a href="../../C04-Cart/Delete.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.5.</b>
                        
                        购物车删除
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.6" data-path="C04-Cart/SelectAll.html">
            
                
                    <a href="../../C04-Cart/SelectAll.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.6.</b>
                        
                        购物车全选
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.7" data-path="C04-Cart/MergeCart.html">
            
                
                    <a href="../../C04-Cart/MergeCart.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.7.</b>
                        
                        登录合并购物车
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="5" data-path="C05-Order/index.html">
            
                
                    <a href="../../C05-Order/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        订单部分
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.1" data-path="C05-Order/OrderDBModel.html">
            
                
                    <a href="../../C05-Order/OrderDBModel.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.1.</b>
                        
                        订单数据库设计
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="C05-Order/PlaceOrder.html">
            
                
                    <a href="../../C05-Order/PlaceOrder.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.2.</b>
                        
                        订单结算
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.3" data-path="C05-Order/SaveOrder.html">
            
                
                    <a href="../../C05-Order/SaveOrder.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.3.</b>
                        
                        保存订单
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.4" data-path="C05-Order/OrderSuccess.html">
            
                
                    <a href="../../C05-Order/OrderSuccess.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.4.</b>
                        
                        下单成功
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="6" data-path="C06-Pay/index.html">
            
                
                    <a href="../../C06-Pay/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                        支付宝支付
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="6.1" data-path="C06-Pay/Alipay.html">
            
                
                    <a href="../../C06-Pay/Alipay.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.1.</b>
                        
                        接入支付宝
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.2" data-path="C06-Pay/Paying.html">
            
                
                    <a href="../../C06-Pay/Paying.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.2.</b>
                        
                        发起支付
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.3" data-path="C06-Pay/PaymentResult.html">
            
                
                    <a href="../../C06-Pay/PaymentResult.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.3.</b>
                        
                        保存支付结果
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="7" data-path="C07-Xadmin.html">
            
                
                    <a href="../../C07-Xadmin.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.</b>
                        
                        Xadmin
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8" data-path="C08-Permission.html">
            
                
                    <a href="../../C08-Permission.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.</b>
                        
                        用户权限控制
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="9" data-path="C09-Database/index.html">
            
                
                    <a href="../../C09-Database/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.</b>
                        
                        数据库读写分离
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="9.1" data-path="C09-Database/MasterSlave.html">
            
                
                    <a href="../../C09-Database/MasterSlave.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.1.</b>
                        
                        主从同步配置
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="9.2" data-path="C09-Database/ReadWriteSeparate.html">
            
                
                    <a href="../../C09-Database/ReadWriteSeparate.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.2.</b>
                        
                        Django数据库读写分离
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="10" data-path="C10-Deploy.html">
            
                
                    <a href="../../C10-Deploy.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.</b>
                        
                        Django部署
                    </a>
            
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    Published with GitBook
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../../" >美多商城</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="qq&#x767B;&#x5F55;&#x56DE;&#x8C03;&#x5904;&#x7406;">QQ&#x767B;&#x5F55;&#x56DE;&#x8C03;&#x5904;&#x7406;</h1>
<p>&#x7528;&#x6237;&#x5728;QQ&#x767B;&#x5F55;&#x6210;&#x529F;&#x540E;&#xFF0C;QQ&#x4F1A;&#x5C06;&#x7528;&#x6237;&#x91CD;&#x5B9A;&#x5411;&#x56DE;&#x6211;&#x4EEC;&#x914D;&#x7F6E;&#x7684;&#x56DE;&#x8C03;callback&#x7F51;&#x5740;&#xFF0C;&#x5728;&#x672C;&#x9879;&#x76EE;&#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x7533;&#x8BF7;QQ&#x767B;&#x5F55;&#x5F00;&#x53D1;&#x8D44;&#x8D28;&#x65F6;&#x914D;&#x7F6E;&#x7684;&#x56DE;&#x8C03;&#x5730;&#x5740;&#x4E3A;&#xFF1A;</p>
<pre><code class="lang-url">http://www.meiduo.site:8080/oauth_callback.html
</code></pre>
<p>&#x5728;QQ&#x5C06;&#x7528;&#x6237;&#x91CD;&#x5B9A;&#x5411;&#x5230;&#x6B64;&#x7F51;&#x9875;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x91CD;&#x5B9A;&#x5411;&#x7684;&#x7F51;&#x5740;&#x4F1A;&#x643A;&#x5E26;QQ&#x63D0;&#x4F9B;&#x7684;code&#x53C2;&#x6570;&#xFF0C;&#x7528;&#x4E8E;&#x83B7;&#x53D6;&#x7528;&#x6237;&#x4FE1;&#x606F;&#x4F7F;&#x7528;&#xFF0C;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x5C06;&#x8FD9;&#x4E2A;code&#x53C2;&#x6570;&#x53D1;&#x9001;&#x7ED9;&#x540E;&#x7AEF;&#xFF0C;&#x5728;&#x540E;&#x7AEF;&#x4E2D;&#x4F7F;&#x7528;code&#x53C2;&#x6570;&#x5411;QQ&#x8BF7;&#x6C42;&#x7528;&#x6237;&#x7684;&#x8EAB;&#x4EFD;&#x4FE1;&#x606F;&#xFF0C;&#x5E76;&#x67E5;&#x8BE2;&#x4E0E;&#x8BE5;QQ&#x7528;&#x6237;&#x7ED1;&#x5B9A;&#x7684;&#x7528;&#x6237;&#x3002;</p>
<h4 id="&#x540E;&#x7AEF;&#x63A5;&#x53E3;&#x8BBE;&#x8BA1;">&#x540E;&#x7AEF;&#x63A5;&#x53E3;&#x8BBE;&#x8BA1;</h4>
<p><strong>&#x8BF7;&#x6C42;&#x65B9;&#x5F0F;</strong> &#xFF1A; GET  /oauth/qq/user/?code=xxx</p>
<p><strong>&#x8BF7;&#x6C42;&#x53C2;&#x6570;</strong>&#xFF1A;  &#x67E5;&#x8BE2;&#x5B57;&#x7B26;&#x4E32;&#x53C2;&#x6570;</p>
<table>
<thead>
<tr>
<th>&#x53C2;&#x6570;</th>
<th>&#x7C7B;&#x578B;</th>
<th>&#x662F;&#x5426;&#x5FC5;&#x4F20;</th>
<th>&#x8BF4;&#x660E;</th>
</tr>
</thead>
<tbody>
<tr>
<td>code</td>
<td>str</td>
<td>&#x662F;</td>
<td>qq&#x8FD4;&#x56DE;&#x7684;&#x6388;&#x6743;&#x51ED;&#x8BC1;code</td>
</tr>
</tbody>
</table>
<p><strong>&#x8FD4;&#x56DE;&#x6570;&#x636E;</strong>&#xFF1A; JSON</p>
<pre><code class="lang-json">{
    <span class="hljs-string">&quot;access_token&quot;</span>: xxxx,
}
&#x6216;
{
    <span class="hljs-string">&quot;token&quot;</span>: <span class="hljs-string">&quot;xxx&quot;</span>,
    <span class="hljs-string">&quot;username&quot;</span>: <span class="hljs-string">&quot;python&quot;</span>,
    <span class="hljs-string">&quot;user_id&quot;</span>: <span class="hljs-number">1</span>
}
</code></pre>
<table>
<thead>
<tr>
<th>&#x8FD4;&#x56DE;&#x503C;</th>
<th>&#x7C7B;&#x578B;</th>
<th>&#x662F;&#x5426;&#x5FC5;&#x987B;</th>
<th>&#x8BF4;&#x660E;</th>
</tr>
</thead>
<tbody>
<tr>
<td>access_token</td>
<td>str</td>
<td>&#x5426;</td>
<td>&#x7528;&#x6237;&#x662F;&#x7B2C;&#x4E00;&#x6B21;&#x4F7F;&#x7528;QQ&#x767B;&#x5F55;&#x65F6;&#x8FD4;&#x56DE;&#xFF0C;&#x5176;&#x4E2D;&#x5305;&#x542B;openid&#xFF0C;&#x7528;&#x4E8E;&#x7ED1;&#x5B9A;&#x8EAB;&#x4EFD;&#x4F7F;&#x7528;&#xFF0C;<strong>&#x6CE8;&#x610F;&#x8FD9;&#x4E2A;&#x662F;&#x6211;&#x4EEC;&#x81EA;&#x5DF1;&#x751F;&#x6210;&#x7684;</strong></td>
</tr>
<tr>
<td>token</td>
<td>str</td>
<td>&#x5426;</td>
<td>&#x7528;&#x6237;&#x4E0D;&#x662F;&#x7B2C;&#x4E00;&#x6B21;&#x4F7F;&#x7528;QQ&#x767B;&#x5F55;&#x65F6;&#x8FD4;&#x56DE;&#xFF0C;&#x767B;&#x5F55;&#x6210;&#x529F;&#x7684;JWT token</td>
</tr>
<tr>
<td>username</td>
<td>str</td>
<td>&#x5426;</td>
<td>&#x7528;&#x6237;&#x4E0D;&#x662F;&#x7B2C;&#x4E00;&#x6B21;&#x4F7F;&#x7528;QQ&#x767B;&#x5F55;&#x65F6;&#x8FD4;&#x56DE;&#xFF0C;&#x7528;&#x6237;&#x540D;</td>
</tr>
<tr>
<td>user_id</td>
<td>int</td>
<td>&#x5426;</td>
<td>&#x7528;&#x6237;&#x4E0D;&#x662F;&#x7B2C;&#x4E00;&#x6B21;&#x4F7F;&#x7528;QQ&#x767B;&#x5F55;&#x65F6;&#x8FD4;&#x56DE;&#xFF0C;&#x7528;&#x6237;id</td>
</tr>
</tbody>
</table>
<h4 id="&#x8F85;&#x52A9;&#x6587;&#x4EF6;">&#x8F85;&#x52A9;&#x6587;&#x4EF6;</h4>
<p>&#x5728;oauth&#x5E94;&#x7528;&#x4E2D;&#x521B;&#x5EFA;constants.py&#x6587;&#x4EF6;</p>
<pre><code class="lang-python"><span class="hljs-comment">#QQ&#x6388;&#x6743;&#x540E;&#xFF0C;&#x8FDB;&#x884C;&#x8D26;&#x53F7;&#x7ED1;&#x5B9A;&#x7684;&#x65F6;&#x95F4;</span>
SAVE_QQ_USER_TOKEN_EXPIRES=<span class="hljs-number">600</span>
</code></pre>
<p>&#x5728;oauth&#x5E94;&#x7528;&#x4E2D;&#x521B;&#x5EFA;exceptions.py&#x6587;&#x4EF6;</p>
<pre><code class="lang-python"><span class="hljs-comment">#&#x81EA;&#x5B9A;&#x4E49;&#x5F02;&#x5E38;&#x7C7B;&#x578B;&#xFF1A;QQ&#x5F02;&#x5E38;</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">QQAPIError</span><span class="hljs-params">(Exception)</span>:</span>
    <span class="hljs-keyword">pass</span>
</code></pre>
<h4 id="oauthqq&#x8F85;&#x52A9;&#x7C7B;&#x5B9E;&#x73B0;">OAuthQQ&#x8F85;&#x52A9;&#x7C7B;&#x5B9E;&#x73B0;</h4>
<p>&#x5728;utils.py&#x4E2D;&#xFF0C;&#x4E3A;&#x7C7B;OAuthQQ&#x6DFB;&#x52A0;&#x65B9;&#x6CD5;</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> .exceptions <span class="hljs-keyword">import</span> QQAPIError
<span class="hljs-keyword">from</span> . <span class="hljs-keyword">import</span> constants

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">OAuthQQ</span><span class="hljs-params">(object)</span>:</span>
    ...

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_access_token</span><span class="hljs-params">(self, code)</span>:</span>
        <span class="hljs-string">&quot;&quot;&quot;
        &#x83B7;&#x53D6;access_token
        :param code: qq&#x63D0;&#x4F9B;&#x7684;code
        :return: access_token
        &quot;&quot;&quot;</span>
        params = {
            <span class="hljs-string">&apos;grant_type&apos;</span>: <span class="hljs-string">&apos;authorization_code&apos;</span>,
            <span class="hljs-string">&apos;client_id&apos;</span>: self.client_id,
            <span class="hljs-string">&apos;client_secret&apos;</span>: self.client_secret,
            <span class="hljs-string">&apos;code&apos;</span>: code,
            <span class="hljs-string">&apos;redirect_uri&apos;</span>: self.redirect_uri
        }
        url = <span class="hljs-string">&apos;https://graph.qq.com/oauth2.0/token?&apos;</span> + urlencode(params)
        response = urlopen(url)
        response_data = response.read().decode()
        data = parse_qs(response_data)
        access_token = data.get(<span class="hljs-string">&apos;access_token&apos;</span>, <span class="hljs-keyword">None</span>)
        <span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> access_token:
            logger.error(<span class="hljs-string">&apos;code=%s msg=%s&apos;</span> % (data.get(<span class="hljs-string">&apos;code&apos;</span>), data.get(<span class="hljs-string">&apos;msg&apos;</span>)))
            <span class="hljs-keyword">raise</span> QQAPIError

        <span class="hljs-keyword">return</span> access_token[<span class="hljs-number">0</span>]

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_openid</span><span class="hljs-params">(self, access_token)</span>:</span>
        <span class="hljs-string">&quot;&quot;&quot;
        &#x83B7;&#x53D6;&#x7528;&#x6237;&#x7684;openid
        :param access_token: qq&#x63D0;&#x4F9B;&#x7684;access_token
        :return: open_id
        &quot;&quot;&quot;</span>
        url = <span class="hljs-string">&apos;https://graph.qq.com/oauth2.0/me?access_token=&apos;</span> + access_token
        response = urlopen(url)
        response_data = response.read().decode()
        <span class="hljs-keyword">try</span>:
            <span class="hljs-comment"># &#x8FD4;&#x56DE;&#x7684;&#x6570;&#x636E; callback( {&quot;client_id&quot;:&quot;YOUR_APPID&quot;,&quot;openid&quot;:&quot;YOUR_OPENID&quot;} )\n;</span>
            data = json.loads(response_data[<span class="hljs-number">10</span>:-<span class="hljs-number">4</span>])
        <span class="hljs-keyword">except</span> Exception:
            data = parse_qs(response_data)
            logger.error(<span class="hljs-string">&apos;code=%s msg=%s&apos;</span> % (data.get(<span class="hljs-string">&apos;code&apos;</span>), data.get(<span class="hljs-string">&apos;msg&apos;</span>)))
            <span class="hljs-keyword">raise</span> QQAPIError
        openid = data.get(<span class="hljs-string">&apos;openid&apos;</span>, <span class="hljs-keyword">None</span>)
        <span class="hljs-keyword">return</span> openid

<span class="hljs-decorator">    @staticmethod</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">generate_save_user_token</span><span class="hljs-params">(openid)</span>:</span>
        <span class="hljs-string">&quot;&quot;&quot;
        &#x751F;&#x6210;&#x4FDD;&#x5B58;&#x7528;&#x6237;&#x6570;&#x636E;&#x7684;token
        :param openid: &#x7528;&#x6237;&#x7684;openid
        :return: token
        &quot;&quot;&quot;</span>
        serializer = Serializer(settings.SECRET_KEY, expires_in=constants.SAVE_QQ_USER_TOKEN_EXPIRES)
        data = {<span class="hljs-string">&apos;openid&apos;</span>: openid}
        token = serializer.dumps(data)
        <span class="hljs-keyword">return</span> token.decode()

<span class="hljs-decorator">    @staticmethod</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">check_save_user_token</span><span class="hljs-params">(token)</span>:</span>
        <span class="hljs-string">&quot;&quot;&quot;
        &#x68C0;&#x9A8C;&#x4FDD;&#x5B58;&#x7528;&#x6237;&#x6570;&#x636E;&#x7684;token
        :param token: token
        :return: openid or None
        &quot;&quot;&quot;</span>
        serializer = Serializer(settings.SECRET_KEY, expires_in=constants.SAVE_QQ_USER_TOKEN_EXPIRES)
        <span class="hljs-keyword">try</span>:
            data = serializer.loads(token)
        <span class="hljs-keyword">except</span> BadData:
            <span class="hljs-keyword">return</span> <span class="hljs-keyword">None</span>
        <span class="hljs-keyword">else</span>:
            <span class="hljs-keyword">return</span> data.get(<span class="hljs-string">&apos;openid&apos;</span>)
</code></pre>
<h4 id="&#x89C6;&#x56FE;&#x5B9E;&#x73B0;">&#x89C6;&#x56FE;&#x5B9E;&#x73B0;</h4>
<p>&#x5728;oauth/views.py&#x4E2D;&#x5B9E;&#x73B0;&#x89C6;&#x56FE;</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> rest_framework.views <span class="hljs-keyword">import</span> APIView
<span class="hljs-keyword">from</span> rest_framework.response <span class="hljs-keyword">import</span> Response
<span class="hljs-keyword">from</span> rest_framework <span class="hljs-keyword">import</span> status
<span class="hljs-keyword">from</span> rest_framework_jwt.settings <span class="hljs-keyword">import</span> api_settings

<span class="hljs-keyword">from</span> .utils <span class="hljs-keyword">import</span> OAuthQQ
<span class="hljs-keyword">from</span> .models <span class="hljs-keyword">import</span> OAuthQQUser
<span class="hljs-keyword">from</span> .exceptions <span class="hljs-keyword">import</span> QQAPIError

<span class="hljs-comment"># url(r&apos;^qq/user/$&apos;,views.QQAuthUserView.as_view()),</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">QQAuthUserView</span><span class="hljs-params">(APIView)</span>:</span>
    <span class="hljs-string">&quot;&quot;&quot;
    QQ&#x767B;&#x5F55;&#x7684;&#x7528;&#x6237;
    &quot;&quot;&quot;</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get</span><span class="hljs-params">(self, request)</span>:</span>
        <span class="hljs-string">&quot;&quot;&quot;
        &#x83B7;&#x53D6;qq&#x767B;&#x5F55;&#x7684;&#x7528;&#x6237;&#x6570;&#x636E;
        &quot;&quot;&quot;</span>
        code = request.query_params.get(<span class="hljs-string">&apos;code&apos;</span>)
        <span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> code:
            <span class="hljs-keyword">return</span> Response({<span class="hljs-string">&apos;message&apos;</span>: <span class="hljs-string">&apos;&#x7F3A;&#x5C11;code&apos;</span>}, status=status.HTTP_400_BAD_REQUEST)

        oauth = OAuthQQ()

        <span class="hljs-comment"># &#x83B7;&#x53D6;&#x7528;&#x6237;openid</span>
        <span class="hljs-keyword">try</span>:
            access_token = oauth.get_access_token(code)
            openid = oauth.get_openid(access_token)
        <span class="hljs-keyword">except</span> QQAPIError:
            <span class="hljs-keyword">return</span> Response({<span class="hljs-string">&apos;message&apos;</span>: <span class="hljs-string">&apos;QQ&#x670D;&#x52A1;&#x5F02;&#x5E38;&apos;</span>}, status=status.HTTP_503_SERVICE_UNAVAILABLE)

        <span class="hljs-comment"># &#x5224;&#x65AD;&#x7528;&#x6237;&#x662F;&#x5426;&#x5B58;&#x5728;</span>
        <span class="hljs-keyword">try</span>:
            qq_user = OAuthQQUser.objects.get(openid=openid)
        <span class="hljs-keyword">except</span> OAuthQQUser.DoesNotExist:
            <span class="hljs-comment"># &#x7528;&#x6237;&#x7B2C;&#x4E00;&#x6B21;&#x4F7F;&#x7528;QQ&#x767B;&#x5F55;</span>
            token = oauth.generate_save_user_token(openid)
            <span class="hljs-keyword">return</span> Response({<span class="hljs-string">&apos;access_token&apos;</span>: token})
        <span class="hljs-keyword">else</span>:
            <span class="hljs-comment"># &#x627E;&#x5230;&#x7528;&#x6237;, &#x751F;&#x6210;token</span>
            user = qq_user.user
            jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER
            jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER

            payload = jwt_payload_handler(user)
            token = jwt_encode_handler(payload)

            response = Response({
                <span class="hljs-string">&apos;token&apos;</span>: token,
                <span class="hljs-string">&apos;user_id&apos;</span>: user.id,
                <span class="hljs-string">&apos;username&apos;</span>: user.username
            })
            <span class="hljs-keyword">return</span> response
</code></pre>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../../C02-Users/QQLogin/Step1.html" class="navigation navigation-prev " aria-label="Previous page: 第一步:获取url"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../../C02-Users/QQLogin/Step3.html" class="navigation navigation-next " aria-label="Next page: 第三步:绑定账户"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../../gitbook/app.js"></script>

    
    <script src="../../gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.js"></script>
    

    
    <script src="../../gitbook/plugins/gitbook-plugin-search/lunr.min.js"></script>
    

    
    <script src="../../gitbook/plugins/gitbook-plugin-search/search.js"></script>
    

    
    <script src="../../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

    
    <script src="../../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"toggle-chapters":{},"highlight":{},"search":{"maxIndexSize":1000000},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        <!-- body:end -->
    </body>
    <!-- End of book 美多商城 -->
</html>
